//
//  PopoverView.m
//  CDatePicker
//
//  Created by Min on 2023/12/10.
//

#import "MKPopoverView.h"
#import "MKMacro.h"
#import "MKPopoverCell.h"
#import "UIColor+Kyes.h"
#import <Masonry.h>

@interface MKPopoverView ()<UITableViewDelegate,UITableViewDataSource>

@property(strong,nonatomic) UITableView *tableView;

@property(assign,nonatomic) NSInteger row_count;
@end


@implementation MKPopoverView

const CGFloat beginX = 22;
static NSString *ID = @"PopoverViewID";

//- (void)drawRect:(CGRect)rect {
//    [super drawRect:rect];
//
//
//    CGRect roundRect = CGRectMake(rect.origin.x, rect.origin.y + 8, rect.size.width, rect.size.height - 8);
//    CGPoint moveP = CGPointMake(rect.size.width * 0.5 - 4, roundRect.origin.y);
//    CGPoint secondP = CGPointMake(rect.size.width * 0.5, 0);
//    CGPoint endP = CGPointMake(rect.size.width * 0.5 + 4, roundRect.origin.y);
//
//    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:roundRect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(6, 6)];
//    [path moveToPoint:moveP];
//    [path addLineToPoint:secondP];
//    [path addLineToPoint:endP];
//    [path closePath];
//    [[UIColor whiteColor] setFill];
//    [path fill];
//}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    
    // 绘制 roundRect
    CGRect roundRect = CGRectMake(rect.origin.x, rect.origin.y + 8, rect.size.width, rect.size.height - 8);
    CGPoint moveP = CGPointMake(rect.size.width * 0.5 - 4, roundRect.origin.y);
    CGPoint secondP = CGPointMake(rect.size.width * 0.5, 0);
    CGPoint endP = CGPointMake(rect.size.width * 0.5 + 4, roundRect.origin.y);
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:roundRect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(6, 6)];
    [path moveToPoint:moveP];
    [path addLineToPoint:secondP];
    [path addLineToPoint:endP];
    [path closePath];
    
    
    // 创建一个CALayer对象，用于添加阴影
    CAShapeLayer *shadowLayer = [CAShapeLayer layer];
    

    // 设置阴影的属性
    shadowLayer.shadowColor = [UIColor blackColor].CGColor;
    shadowLayer.shadowOffset = CGSizeMake(0, 2); // 阴影的偏移量
    shadowLayer.shadowOpacity = 0.5; // 阴影的透明度
    shadowLayer.shadowRadius = 1; // 阴影的半径
    shadowLayer.fillColor = [UIColor hexColor:@"#AAAAAA"].CGColor;
    
    // 将阴影Layer添加到视图的layer中
    [self.layer insertSublayer:shadowLayer atIndex:0];
    shadowLayer.path = path.CGPath;
    
}


- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]){
        self.tableView.rowHeight = 45;
        [self addSubview:self.tableView];
        
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(10);
            make.bottom.equalTo(self).offset(-10);
            make.left.right.equalTo(self);
        }];
        
        self.row_count = 150;
    }
    return self;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.row_count;
}

// 1900
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MKPopoverCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    
    if (self.row_count > 12){
        cell.numLabel.text = [NSString stringWithFormat:@"%ld",1900 + indexPath.item];
    }else{
        cell.numLabel.text = [NSString stringWithFormat:@"%ld",1 + indexPath.item];
    }
    

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [[[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleMedium] impactOccurred];
    if (self.row_count > 12){
        self.select_pop(1900 + indexPath.item);
    }else{
        self.select_pop(1 + indexPath.item);
    }
    
}



- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        [_tableView registerClass:[MKPopoverCell class] forCellReuseIdentifier:ID];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}


-(void)setYearMode:(BOOL)yearMode{
    _yearMode = yearMode;
    if (yearMode){
        self.row_count = 151;
    }else{
        self.row_count = 12;
    }
    [self.tableView reloadData];
    
}

@end
